<template>
  <div>
    <div class="header">
      <div class="logo">
        <img src="http://img.cixi518.com/ljh_logo.jpeg" alt="default_logo">
      </div>
    </div>
    <div class="content">
      <van-field
        placeholder="身份证号"
        v-model="loginForm.regIdCard"
        left-icon="idcard"/>
      <van-field
        placeholder="账户名"
        v-model="loginForm.username"
        left-icon="phone-o"
      />
      <!-- eye -->
      <van-field v-if="loginWay==='password'" placeholder="登录密码" v-model="loginForm.password" left-icon="lock" :type="passwordType">
        <van-icon slot="right-icon" @click="switchPasswordType" :name="passwordIcon"/>
      </van-field>
      <van-field v-else placeholder="短信验证码" v-model="loginForm.password" left-icon="comment-o" type="text">
        <VerifyCodeBtn slot="button" @sendVerifyCode="sendVerifyCode" />
      </van-field>
      <div class="button-wrap">
        <van-button size="large" :loading="loading" @click="handleLogin" type="info">{{ $t('login.logIn') }}</van-button>
      </div>
      <div class="more-wrap">
        <div class="switch-way" @click="switchLoginWay">{{loginWayObj.toggleMsg}}</div>
      </div>
    </div>
  </div>
</template>
<script lang="ts">
import { Component, Vue, Watch } from 'vue-property-decorator'
import { Route } from 'vue-router'
import { Dictionary } from 'vue-router/types/router'
import { Field, Icon, Button, Toast } from 'vant'
import VerifyCodeBtn from '@/components/VerifyCodeBtn.vue'
import { UserModule } from '@/store/modules/user'
import { isValidRegIdCard } from '@/utils/validate'

@Component({
  name: 'Login',
  components: {
    [Field.name]: Field,
    [Icon.name]: Icon,
    [Button.name]: Button,
    VerifyCodeBtn
  }
})
export default class extends Vue {
  private redirect?: string
  private otherQuery: Dictionary<string> = {}
  private loading = false
  private loginForm:any = {
    regIdCard: '',
    username: 'admin',
    password: '123456'
  }
  private code = ''
  private loginWay= 'password'
  private passwordType= 'password'
  private sendVerifyCode () {
    if (!this.loginForm.username) {
      Toast('用户名必填')
    }
  }
  private switchPasswordType () {
    this.passwordType = this.passwordType === 'password' ? 'text' : 'password'
  }
  private switchLoginWay () {
    this.loginForm.password = this.code = ''
    this.loginWay = this.loginWay === 'password' ? 'verifyCode' : 'password'
  }
  mounted () {
  }
  @Watch('$route', { immediate: true })
  private onRouteChange (route: Route) {
    const query = route.query as Dictionary<string>
    if (query) {
      this.redirect = query.redirect
      this.otherQuery = this.getOtherQuery(query)
    }
  }
  private async handleLogin (values:any) {
    let _this = this
    // if (!_this.loginForm.regIdCard) { // 根据需求做判断
    //   Toast('请输入身份证号')
    //   return
    // }
    // if (!isValidRegIdCard(_this.loginForm.regIdCard)) { // 根据需求做判断
    //   Toast('请输入正确的身份证号')
    //   return
    // }
    if (!_this.loginForm.username) {
      Toast('用户名必填')
      return
    }
    if (!_this.loginForm.password) {
      Toast('密码必填')
      return
    }
    _this.loading = true
    try {
      await UserModule.Login(_this.loginForm)
      _this.$router.push({
        path: _this.redirect || '/',
        query: _this.otherQuery
      })
    } finally {
      setTimeout(() => {
        this.loading = false
      }, 0.5 * 1000)
    }
  }
  private getOtherQuery (query: Dictionary<string>) {
    return Object.keys(query).reduce((acc, cur) => {
      if (cur !== 'redirect') {
        acc[cur] = query[cur]
      }
      return acc
    }, {} as Dictionary<string>)
  }
  get loginWayObj () {
    if (this.loginWay === 'password') {
      return {
        icon: 'closed-eye',
        toggleMsg: '验证码登录'
      }
    }
    return {
      icon: 'eye',
      toggleMsg: '密码登录'
    }
  }
  get passwordIcon () {
    return this.passwordType === 'password' ? 'closed-eye' : 'eye'
  }
}
</script>
<style lang="scss" scoped>
  .header{
    width: 100%;
    height: 165px;
    display: flex;
    justify-content: center;
    align-items: center;
    .logo{
      width: 60px;
      height: 60px;
      border-radius: 50%;
      overflow: hidden;
      img{
        width: 100%;
      }
    }
  }
  .content{
    width: 100%;
    height: auto;
    padding: 0 15px;
    box-sizing: border-box;
    .button-wrap{
      width: 100%;
      height: auto;
      margin-top: 15px;
    }
  }
  .more-wrap{
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-top: 15px;
    font-size: 14px;
    a.link{
      color: #1989fa;
    }
    .switch-way{
      color: #333;
    }
  }

</style>
